﻿/***************************************************************************
 * 文 件 名:   HK_RotTargetItem
 * 
 * 描    述:   Camera多目标切换管理工具，RotTargetItem 控制脚本
 * 
 * 版    本：  V1.1
 * 创 建 者：  京产肠饭、谭潇杰
 * 创建时间：  2023/5/9
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
****************************************************************************/

using System;
using UnityEngine;

namespace HKTools
{
    public class HK_RotTarget : HK_BaseTarget
    {
        public Vector3 targetPos
        {
            get { return transform.position + transform.forward * targetDis; }
        }

        public Vector3 targetDir
        {
            get { return transform.position - targetPos; }
        }

        [Header("位置标记的半径：")]
        public float targetRadius = 1f;

        [Header("默认距离（Green）：")]
        public float targetDis = 2;
        [Header("最近距离（Blue）：")]
        public float minDis;
        [Header("最远距离（Red）：")]
        public float maxDis;

        [Header("边界高度："), SerializeField, Range(10, 20)]
        float height = 15;
        [Header("边界长度："), SerializeField, Range(10, 20)]
        float length = 15;

        [Header("左边界角度："), Range(0, 180)]
        public float leftRot;
        [Header("右边界角度："), Range(0, 180)]
        public float rightRot;

        [HideInInspector]
        public bool isChangeTargetEnum = false;

        [HideInInspector]
        public string targetNameCache = null;

        // 绿色为默认目标位置，红色为最远位置，蓝色为最近位置
        void OnDrawGizmosSelected()
        {
            if (minDis > targetDis)
                minDis = targetDis;

            Gizmos.color = Color.blue;
            Gizmos.DrawRay(transform.position, transform.forward * minDis);
            Gizmos.DrawSphere(transform.position + transform.forward * minDis, targetRadius);

            Gizmos.color = Color.green;
            Gizmos.DrawRay(transform.position + transform.forward * minDis, transform.forward * (targetDis - minDis));
            Gizmos.DrawSphere(transform.position + transform.forward * targetDis, targetRadius);

            if (maxDis < targetDis)
                maxDis = targetDis;

            Gizmos.color = Color.red;
            Gizmos.DrawRay(transform.position + transform.forward * targetDis, transform.forward * (maxDis - targetDis));
            Gizmos.DrawSphere(transform.position + transform.forward * maxDis, targetRadius);

            //画摄像机边界
            //画线范围
            if (leftRot != 0 || rightRot != 0)
            {
                DrawWireSemicircle(transform.position, Vector3.ProjectOnPlane(transform.forward, Vector3.up), -leftRot, rightRot, length, height, Vector3.up);
            }
        }

        //绘制左右角度
        public void DrawWireSemicircle(Vector3 origin, Vector3 direction, float leftAngle, float rightAngle, float length, float height, Vector3 axis)
        {
            Gizmos.color = Color.red;
            //左侧
            Vector3 leftdir = Quaternion.AngleAxis(-Mathf.Abs(leftAngle), axis) * direction;
            //右侧
            Vector3 rightdir = Quaternion.AngleAxis(rightAngle, axis) * direction;

            Vector3 curPoint = origin + leftdir.normalized * length;

            // 绘制左面片
            Gizmos.DrawLine(origin, origin + Vector3.up * height);
            Gizmos.DrawLine(origin + Vector3.up * height, curPoint + Vector3.up * height);
            Gizmos.DrawLine(origin, curPoint);
            Gizmos.DrawLine(curPoint + Vector3.up * height, curPoint);

            curPoint = origin + rightdir.normalized * length;

            // 绘制右面片
            Gizmos.DrawLine(origin, origin + Vector3.up * height);
            Gizmos.DrawLine(origin + Vector3.up * height, curPoint + Vector3.up * height);
            Gizmos.DrawLine(origin, curPoint);
            Gizmos.DrawLine(curPoint + Vector3.up * height, curPoint);
        }
    }
}